package com.example.android4_lx;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    Button btn_bind, btn_unbind, btn_getServiceStatus,btn_call,btn_message;
    // 保持所启动的Service的IBinder对象
    BindService.MyBinder binder;

    final Intent intent = new Intent();

    // 定义一个ServiceConnection对象
    private ServiceConnection conn = new ServiceConnection(){
        @Override
        // 当该Activity与Service连接成功时回调该方法
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("Service","is connected");
            //获取Service的onBind方法所返回的MyBinder
            binder = (BindService.MyBinder)service;
        }

        @Override
        // 当该Activity与Service断开连接时回调该方法
        public void onServiceDisconnected(ComponentName name) {
            Log.d("Service","is disconnected");
        }
    };
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_bind = (Button) findViewById(R.id.btn_bind);
        btn_unbind = (Button) findViewById(R.id.btn_unbind);
        btn_call=(Button) findViewById(R.id.btn_call);
        btn_message=(Button) findViewById(R.id.btn_message);
        btn_getServiceStatus = (Button) findViewById(R.id.btn_getServiceStatus);

        //创建启动Service的Intent
        final Intent call_intent=new Intent().setAction(Intent.ACTION_DIAL);
        final Intent send_mess=new Intent().setAction(Intent.ACTION_SEND);

        //为Intent设置Action属性
        intent.setAction("android.intent.action.BIND_SERVICE");
        intent.setPackage(getPackageName());

        btn_bind.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View source){
                //绑定指定Service
                bindService(intent,conn,BIND_AUTO_CREATE);//绑定服务
                startService(intent);
            }
        });
        btn_unbind.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View source){
                //解除绑定Service
                unbindService(conn);//解绑服务
            }
        });
        btn_getServiceStatus.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View source){
                // 获取、并显示Service的count值
                Toast.makeText(MainActivity.this
                        , "Service的count值为：" + binder.getCount()
                        , Toast.LENGTH_SHORT).show();
            }
        });
        btn_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(call_intent);
            }
        });
        btn_message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(send_mess);
            }
        });
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        stopService(intent);
    }
}

